package com.googlecode.animator.view.topmenu
{
	import com.googlecode.animator.data.Notifications;
	import com.googlecode.animator.data.markers.KeyframeVO;
	import com.googlecode.animator.data.materials.BitmapFileMaterialVO;
	import com.googlecode.animator.data.objects3d.CubeVO;
	import com.googlecode.animator.data.objects3d.Object3DVO;
	import com.googlecode.animator.data.objects3d.PlaneVO;
	import com.googlecode.animator.model.ProjectSettingsProxy;
	import com.googlecode.animator.utils.UniqueID;
	
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * Integrates the TopMenu view component with the PureMVC framework.
	 * 
	 * Note: Instances of this Mediator may be dynamically created and removed by the application. 
	 * @author Martin
	 */
	public class CreateButtonsMediator extends Mediator
	{
		/**
		 * PureMVC identifier. 
		 */
		public static const NAME:String = "CreateButtonsMediator";
			 
		/**
		 * Constructor. 
		 * @param viewComponent
		 * 
		 */
		public function CreateButtonsMediator(viewComponent:TopMenu)
		{
			super(NAME, viewComponent);
		}
		
		//////////////////////////////////////////////////////////////////////
		//
		// PureMVC
		//
		//////////////////////////////////////////////////////////////////////
		
		/**
		 * Sets up event listeners to handle menu interactions. 
		 * 
		 */
		override public function onRegister():void
		{
			viewRef.addEventListener(TopMenu.CREATE_PLANE, onCreatePlane);
			viewRef.addEventListener(TopMenu.CREATE_SPHERE, onCreateSphere);
			viewRef.addEventListener(TopMenu.CREATE_CUBE, onCreateCube);
		}
		
		/**
		 * Clean up. 
		 * 
		 */
		override public function onRemove():void
		{
			viewRef.removeEventListener(TopMenu.CREATE_PLANE, onCreatePlane);
			viewRef.removeEventListener(TopMenu.CREATE_SPHERE, onCreateSphere);
			viewRef.removeEventListener(TopMenu.CREATE_CUBE, onCreateCube);
		}
		
		
		
		
		
		//////////////////////////////////////////////////////////////////////
		//
		// Event handlers
		//
		//////////////////////////////////////////////////////////////////////
		
		/**
		 * Handles user clicking on Plane button. 
		 * @param evt
		 * 
		 */
		protected function onCreatePlane(evt:Event):void
		{
			// make new VO to create plane
			var VO:PlaneVO = new PlaneVO();
			
			var front:BitmapFileMaterialVO = new BitmapFileMaterialVO();

			front.filePath = "com/googlecode/animator/assets/front.jpg";
			front.face = PlaneVO.FRONT_FACE;
			front.doubleSided = true;
			
			VO.material = front;
			
			sendNotification(Notifications.ADD_OBJECT3D, VO);
		}
		
		/**
		 * Handles user clicking on Sphere button. 
		 * @param evt
		 * 
		 */
		protected function onCreateSphere(evt:Event):void
		{
			trace(evt.target+"s");
		}
		
		/**
		 * Handles user clicking on Cube button. 
		 * @param evt
		 * 
		 */
		protected function onCreateCube(evt:Event):void
		{	
			var cube:CubeVO = new CubeVO();
			
			var front:BitmapFileMaterialVO = new BitmapFileMaterialVO();
			front.filePath = "com/googlecode/animator/assets/front.jpg";
			front.face = CubeVO.FRONT_FACE;
			
			var back:BitmapFileMaterialVO = new BitmapFileMaterialVO();
			back.filePath = "com/googlecode/animator/assets/back.jpg";
			back.face = CubeVO.BACK_FACE;
			
			var top:BitmapFileMaterialVO = new BitmapFileMaterialVO();
			top.filePath = "com/googlecode/animator/assets/top.jpg";
			top.face = CubeVO.TOP_FACE;
			
			var bottom:BitmapFileMaterialVO = new BitmapFileMaterialVO();
			bottom.filePath = "com/googlecode/animator/assets/bottom.jpg";
			bottom.face = CubeVO.BOTTOM_FACE;
			
			var left:BitmapFileMaterialVO = new BitmapFileMaterialVO();
			left.filePath = "com/googlecode/animator/assets/left.jpg";
			left.face = CubeVO.LEFT_FACE;
			
			var right:BitmapFileMaterialVO = new BitmapFileMaterialVO();
			right.filePath = "com/googlecode/animator/assets/right.jpg";
			right.face = CubeVO.RIGHT_FACE; 
			
			var materialsList:Dictionary = new Dictionary();
			materialsList[CubeVO.FRONT_FACE] = front;
			materialsList[CubeVO.BACK_FACE] = back;
			materialsList[CubeVO.TOP_FACE] = top;
			materialsList[CubeVO.BOTTOM_FACE] = bottom;
			materialsList[CubeVO.LEFT_FACE] = left;
			materialsList[CubeVO.RIGHT_FACE] = right; 
			
			cube.materialsList = materialsList;
			
			sendNotification(Notifications.ADD_OBJECT3D, cube);
		}
		
		/////////////////////////////////////////////////////////////////
		// Utility Functions
		/////////////////////////////////////////////////////////////////
		
		protected function get viewRef():TopMenu
		{
			return viewComponent as TopMenu;
		}
		
		protected function get settings():ProjectSettingsProxy
		{
			return facade.retrieveProxy(ProjectSettingsProxy.NAME) as ProjectSettingsProxy;
		}
	}
}